import { BrowserWindow, ipcMain } from "electron";
import { winparams } from "./params";
import { logger, WINDOW_PORT, RENDER_HTML_PATH } from "@/settings";
import { setWindowById } from "@/jc-electron-tools/windows";
import * as remote from "@electron/remote/main";

function newWinUser() {
  const window = new BrowserWindow(winparams as any);
  const id = setWindowById(window);
  remote.enable(window.webContents);

  // 用于渲染进程获取 user window 的 id
  ipcMain.on('get-user-window', (event) => {
    event.reply('user-window', id);
  });
  
  if (process.env.NODE_ENV === "development") {
    logger.debug(
      `[New Window] Start Main Window At URL: http://localhost:${WINDOW_PORT.user}/`
    );
    window.loadURL(` http://127.0.0.1:${WINDOW_PORT.user}/`);
  } else {
    window.loadFile(`${RENDER_HTML_PATH.user}`);
  }
  window.webContents.openDevTools();

  return id;
}

export { newWinUser };
